/** 创建组件对象 * */
var roleForm = new Form();
var roleTable = new Table();
var rolePage = new Page();
var roleDialog = new Dialog();
var roleAjax = new Ajax();
var indicatorPackageTree = new Tree();

/** 页面加载完执行 * */
$(function() {
	initScript();
	initScripts();

	initButtons();
	initTree();

	initSelectDiv();
	initRegultDiv();
	initUnitDiv();
	initButton();

	initIndicatorGroupTree();

	setElementsEnabled("dimension", "select", false);
	disableButton();
});

/** 初始化script * */
function initScript() {
	loadScript(appPath + "/framework-system/role/locale/Role_" + appLocale
			+ ".js"); // 1.加载国际化js*
	loadScript(appPath + "/framework-system/auths/locale/Auth_" + appLocale
			+ ".js");
	loadScript(appPath
			+ "/project-yddemo/indicatorPackage/locale/IndicatorPackage_"
			+ appLocale + ".js");
	loadScript(appPath + "/project-yddemo/indicatorRole/locale/IndicatorRole_"
			+ appLocale + ".js");
	loadScript(appPath
			+ "/project-yddemo/indicatorPackage/properties/IndicatorPackageProperties_Main.js"); // 2.加载配置js(非必配,配置也可写在本js中)
	loadScript(appPath
			+ "/project-yddemo/indicatorPackage/controller/IndicatorPackageController_Data.js"); // 2.加载配置js(非必配,配置也可写在本js中)
}
function disableIndicatorButton() {
	setElementsEnabled("yddemo:indicatorPackage:edit", "button", false);
	setElementsEnabled("yddemo:indicatorPackage:delete", "button", false);
	setElementsEnabled("yddemo:indicatorRole:save", "button", false);
}
function disableButton() {
	setElementsEnabled("yddemo:indicatorPackage:edit", "button", false);
	setElementsEnabled("yddemo:indicatorPackage:delete", "button", false);
	setElementsEnabled("yddemo:indicatorRole:save", "button", false);
}
function enableButton() {
	setElementsEnabled("yddemo:indicatorPackage:edit", "button", true);
	setElementsEnabled("yddemo:indicatorPackage:delete", "button", true);
	setElementsEnabled("yddemo:indicatorRole:save", "button", true);
	$("[action='yddemo:indicatorPackage:edit']").unbind("click").bind("click",
			function() {
		updateIndicatorPackage();
			});
	$("[action='yddemo:indicatorPackage:delete']").unbind("click").bind(
			"click", function() {
				deleteIndicatorPackage();
			});
	$("[action='yddemo:indicatorRole:save']").unbind("click").bind(
			"click", function() {
				save();
			});
}
function initButtons() {
	roleTable.initButtons({
		divId : "indicatorRoleQueryDiv", // 1.归属div*
		buttons : IndicatorPackageProperties.getViewButtons()
	});disableButton();
//	setElementsEnabled("yddemo:indicatorPackage:add", "button", false);
	
}

function updateTreeNode(nodeId) {
	// 刷新树法一
	var nodeObj = indicatorPackageTree.getNode(nodeId);
	nodeObj.expanded = false;
	nodeObj.children = null;
	indicatorPackageTree.reloadTree();
	indicatorPackageTree.expandDefaultFun(nodeId);
}
/** 分页查询 * */
function initTree() {
	indicatorPackageTree
			.initTree({
				divId : "indicatorRoleTableDiv", // 归属div*
				ajaxType : "param", // ajax:初始化url/root_root,懒加载url/nodeId);
				// param:初始化url?node=root_root,懒加载url?node=nodeId
				root : {
					text : groupLocale.text.groupRoot,
					nodeId : "root_root",
					leaf : false,
					expanded : true
				},
				dataAjax : { // 数据源ajax*
					url : requestPath + "/yddemo/indicatorPackageTree", // 请求url*
					async : false, // 是否异步*
					type : "GET", // 请求类型*
					params : [],
					data : []
				},
				models : IndicatorRoleData.getTreeModels(indicators),
				align : { // 元素顺序
					left : "expand",
					center : "text",
					right : "check"
				},
				checkIcon : staticPath
						+ "/framework/default/img/Icon_tree_check.png", // 选中图标
				partcheckIcon : staticPath
						+ "/framework/default/img/Icon_tree_partcheck.png", // 部分选图标
				uncheckIcon : staticPath
						+ "/framework/default/img/Icon_tree_uncheck.png", // 未选图标
				expandIcon : staticPath
						+ "/framework/default/img/Icon_tree_expand.png", // 展开图标
				closeIcon : staticPath
						+ "/framework/default/img/Icon_tree_close.png", // 闭合图标
				textClickFun : function(nodeId) { // 文本单击事件*
					// 文本单击事件只操作右边div的显示的tab项
					if (nodeId == "undefined") {
						nodeId = "root_root";
					}
					indicatorPackageTree.checkDefaultFun(nodeId);// 添加
					groupPageQuery(nodeId);
					initDatas(nodeId);
				},
				expandFun : function(nodeId) { // 展开单击事件*
					// 展开单击事件只对树进行展开和合并操作
					if (nodeId == "undefined") {
						nodeId = "root_root";
					}
					indicatorPackageTree.expandDefaultFun(nodeId);
					groupPageQuery(nodeId);
					initDatas(nodeId);

				},
				checkFun : function(nodeId) { // 选中单击事件*
					if (nodeId == "undefined") {
						nodeId = "root_root";
					}
					indicatorPackageTree.checkDefaultFun(nodeId);
					groupPageQuery(nodeId);
					initDatas(nodeId);
				}
			});
}


/** 新增 * */
function addIndicatorPackage() {

		roleDialog.iframeDialog(requestPath + "/yddemo/indicatorPackages/page/add",
				roleLocale.text.addRole, 400, 330, "yddemo:indicatorPackage:add");

}

/** 修改 * */
function updateIndicatorPackage() {
	updateData();
}
function updateData() {
	roleDialog.iframeDialog(requestPath + "/yddemo/indicatorPackages/page/edit",
			roleLocale.text.editRole, 400, 330, "yddemo:indicatorPackage:edit");
}

/** 删除 * */
function deleteIndicatorPackage() {
	var selectDatas = roleTable.getSelectDatas();
	var ids = selectDatas.map(function(item){
		return item.id;
	});

	deleteData(ids);
}
function deleteData(ids) {
	roleAjax = new Ajax();
	roleDialog.confirmDialog(roleLocale.text.isDelete, function() {
		var resultJson = roleAjax.sendAjax({
			url : requestPath + "/system/roles",
			data : ids,
			type : "DELETE"
		});
		if (resultJson.success) {
			roleDialog.tipDialog(roleLocale.text.deleteSuccess, function() {
				pageQuery()
			});
		} else {
			roleDialog.alertDialog(resultJson.message); // 删除失败后,弹出警告框
		}
	});

}
function addPageControlPackage() {
	roleDialog;
}
function editPageControlPackage() {
	roleDialog;
}
function deletePageControlPackage() {
	roleDialog;
}

function refreshIndicator() {
	var params = {
		groupId : sessionStorage.groupId
	};
	sessionStorage.indicatorPackageParams=params;
	//indicatorTable.reloadData({}, params);
	disableIndicatorButton();
}

/**
 * 重设表格高度
 * 
 * @param
 * @return
 */
function resetTableHeight() {
	var leftHeight = $(".dataListDiv").height()
			- getTotalHeight([ "indicatorRoleQueryDiv", "indicatorRolePageDiv" ]);
	$("#indicatorRoleTableDiv").height(leftHeight);
}

/**
 * 窗口大小改变时触发
 * 
 * @param
 * @return
 */
$(window).resize(function() {
	resetTableHeight();
	
});